using System;
using System.Collections.Generic;
using System.Linq;
using Jink.Protocol.Core;

namespace Jink.Protocol
{
    public class JinkProtocol
    {
        private readonly List<Tuple<Func<IRequestMedia, bool>, IProtocolModule>> _pipeline = new List<Tuple<Func<IRequestMedia, bool>, IProtocolModule>>();
        public List<Tuple<Func<IRequestMedia, bool>, IProtocolModule>> Pipeline { get { return _pipeline; } }

        public void Add(Func<IRequestMedia, bool> filter, IProtocolModule module)
        {
            Pipeline.Add(Tuple.Create(filter, module));
        }

        public void Process(IRequestMedia requestMedia, IResponseMedia responseMedia)
        {
            var module = Pipeline.Single(pair => pair.Item1(requestMedia));
            var logicalRequest = module.Item2.Process(requestMedia);
            var logicalResponse = logicalRequest.Respond();
            logicalResponse.DoSend(responseMedia);
        }
    }
}